package com.yuxi.Dao.Impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.yuxi.Dao.PrisonStructureDao;
import com.yuxi.Entity.Position;
import com.yuxi.Entity.PrisonStructure;
import com.yuxi.Entity.UserInfo;
import com.yuxi.Entity.UserPosition;

@Repository
public class PrisonStructureDaoImpl extends BaseDaoImpl<Serializable, Serializable> implements PrisonStructureDao {
	private Logger logger=LoggerFactory.getLogger(PrisonStructureDaoImpl.class);
	public PrisonStructureDaoImpl(){
		super(PrisonStructure.class);
	}
	@Override
	public PrisonStructure getStructure(Integer pStructureId) {
		// TODO Auto-generated method stub
		return (PrisonStructure) super.get(pStructureId);
	}
	@Override
	public PrisonStructure addStructure(PrisonStructure pStructure) {
		// TODO Auto-generated method stub
		return (PrisonStructure) super.addAndReturn(pStructure);
	}
	@Override
	public void deleteStructure(PrisonStructure pStructure) {
		// TODO Auto-generated method stub
		super.delete(pStructure);
	}
	@Override
	public void modifly(PrisonStructure pStructure) {
		// TODO Auto-generated method stub
		super.update(pStructure);
	}
	@Override
	public List<PrisonStructure> getAllStructure() {
		List<PrisonStructure> pStructures=null;
		return pStructures= super.find(" from PrisonStructure");
	}
	@Override
	public List<PrisonStructure> getAllStructureBySuperId(Integer superId) {
		List<PrisonStructure> pStructures=null;
		return pStructures= super.find("from PrisonStructure where superId=?", superId);
	}
	@Override
	public List<PrisonStructure> getAllSuperStructure() {
		List<PrisonStructure> pStructures=null;
		return pStructures= super.find("from PrisonStructure where superId=? ", 0);
	}
	@Override
	public List<UserInfo> getStructureALLUser(Integer structId) {	
		if(structId!=null){
			String string=getAllStructureIdById(structId);
			String structStr=" ";
			System.out.println("11111111111111L:"+string);
			if (structId==1) {
				structStr=" ";
			}else{
				structStr=" and usTable.structureId in("+string+")";
			}
			return super.find("select new UserInfo(utable.userId, utable.userName, utable.userAccount)"
					+ " from UserInfo as utable,  UserStructure  as usTable where     usTable.userId=utable.userId  "+structStr);
		}else{
			return super.find("select new UserInfo(utable.userId, utable.userName, utable.userAccount)"
					+ " from UserInfo as utable,  UserStructure  as usTable where    usTable.userId=utable.userId ");
		}
	}
	@Override
	public List<PrisonStructure> getAllPrisonArea() {
		List<PrisonStructure> pStructures=null;
		return pStructures= super.find("from PrisonStructure where superId=? and value=?", 0,"PrisonArea");
	}
	@Override
	public List<PrisonStructure> getDepartmentAndPrisonArea() {
		List<PrisonStructure> pStructures=null;
		return pStructures= super.find("from PrisonStructure where  value=? or value=? order by num desc", "Department","PrisonArea");
	}
	@Override
	public List<PrisonStructure> getAllPrisonStructure() {
		return super.find("from PrisonStructure where  value=? ", "Prison");
	}
	@Override
	public List<PrisonStructure> getAllPrisonAndAreaStructure() {
		return super.find("from PrisonStructure where  value=? or value=?", "Prison","PrisonArea");
	}
	@Override
	public List<PrisonStructure> getAllPrisonAreaStructure() {
		return super.find("from PrisonStructure where  value=? ", "PrisonArea");
	}
	@Override
	public List<PrisonStructure> getAllQuestionStructure() {
		// TODO Auto-generated method stub
		return super.find("from PrisonStructure where typeName is null ");
	}
	private  String getAllStructureIdById(Integer id){
		List<PrisonStructure> prisonStructures=new ArrayList<PrisonStructure>();
		StringBuffer sBuffer= new StringBuffer();
		String str=sBuffer.append(id+",").toString();
		prisonStructures = super.find("select id from PrisonStructure where  superId=? ", id);
		if (prisonStructures.size()!=0) {
			str=str+getString(prisonStructures);	
			
		}
		str=str.substring(0, str.length()-1);
		
		
		
		
		
		return str;
	}
	private String  getString(List<PrisonStructure> prisonStructures){
		StringBuffer sBuffer= new StringBuffer();
		
		for (int i = 0; i < prisonStructures.size(); i++) {
			sBuffer.append(prisonStructures.get(i)+",");
		}
		
		return sBuffer.toString();
	}
}
